//
//  InputViewController.m
//  xingfugangwan
//
//  Created by rainday on 15/1/1.
//  Copyright (c) 2015年 rainday. All rights reserved.
//

#import "InputViewController.h"
#import <ReactiveCocoa.h>
#import <RACEXTScope.h>
#import "AFNetworking.h"
#import "NetIP.h"

#define HOTEL @[@"下元金伯爵",@"基地金伯爵",@"长风街金伯爵",@" 朝阳金伯爵",@"世界金伯爵",@"江南四部",@" 万狮京华",@" 海世界",@"石家庄好运来",@"黄河京都",@" 迎泽宾馆",@"金港",@" 翡翠"]
#define TING @[@[@"中国风",@"巴厘岛",@"华彩厅",@"童话世界",@"普罗旺斯",@"迎喜厅"],@[@" 桃花源",@"维也纳",@" 爱琴海",@"伯爵厅",@"空中花园"],@[@"普罗旺斯",@" 伯爵厅"],@[@"巴黎厅",@"罗马厅",@"中国风"],@[@"绿野仙踪",@"童话世界",@"普罗旺斯",@"中国风",@"天鹅湖",@" 伯爵厅"],@[@"宴会厅",@"多功能厅"],@[@"帷景厅",@"京华厅",@"会议中心"],@[@"二楼东厅",@"二楼南厅",@"三楼",@"四楼"],@[@"好运厅",@"鸿运厅",@"悦厅",@"海悦厅",@"如意厅",@" 福运厅",@" 好合厅"],@[@"水晶宫",@"玫瑰厅1",@"玫瑰厅2",@"金色大厅",@"北京厅",@"玉兰厅",@"运城厅",@"京都厅"],@[@"迎泽厅",@"晋泽轩",@"迎泽二楼"],@[@"35层"],@[@"一层南厅",@"一层北厅",@"三层"]]

#define PAY @[@"现金支付",@"POS支付"]


@interface InputViewController ()<UITextFieldDelegate,UIPickerViewDataSource,UIPickerViewDelegate>{
    InputViewControllerTextFieldStyle _textFieldStyle;
    NSString *_placeHolderText;
    NSString *_textFieldText;
    NSInteger _row;
    NSArray *_saleMenArray;
    
    
    
}

@end

@implementation InputViewController


- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil textFieldText:(NSString *)textFieldText textFieldPlaceholder:(NSString *)textFieldPlaceholder textFieldStyle:(InputViewControllerTextFieldStyle)textFieldStyle
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    _textFieldText = textFieldText;
    _placeHolderText = textFieldPlaceholder;
    _textField.delegate = self;
    _textFieldStyle = textFieldStyle;
    _row = 0;
    
    if (_textFieldStyle == InputViewControllerTextFieldSaleMan) {
        
        [self getSaleMen];
       
    }
    
    
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _textField.text = _textFieldText;
    _textField.placeholder = _placeHolderText;
    _textField.delegate =self;
    
    UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(popVC)];
    
    self.navigationItem.leftBarButtonItem = barItem;
    
    
    
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    if (_textFieldStyle == InputViewControllerTextFieldNormal) {
        return;
    }
    if (_textFieldStyle == InputViewControllerTextFieldPrice) {
        textField.keyboardType = UIKeyboardTypeNumberPad;
        return;
    }
    
    if (_textFieldStyle== InputViewControllerTextFieldDate) {
        
        UIDatePicker *datePicker = [[UIDatePicker alloc] init];
        NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh-CN"];
        datePicker.locale = locale;
        datePicker.datePickerMode = UIDatePickerModeDate;
        
        datePicker.timeZone = [NSTimeZone timeZoneWithName:@"GMT"];
        datePicker.timeZone = [NSTimeZone timeZoneWithName:@"GMT"];
        textField.inputView = datePicker;
        
       
        
        [[datePicker rac_signalForControlEvents:UIControlEventValueChanged] subscribeNext:^(UIDatePicker *datePicker) {
        
            NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        
        
            [dateFormatter setDateFormat:@"yyyy-MM-dd"];
        
        
            textField.text = [dateFormatter stringFromDate:datePicker.date];
            
        
        }];
        
                // 建立 UIToolbar
        UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
        UIBarButtonItem *right = [[UIBarButtonItem alloc]init];
        [right setStyle:UIBarButtonItemStyleDone];
        right.title = NSLocalizedString(@"完成", nil);
        
        right.rac_command = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
                [textField resignFirstResponder];
                // An empty signal is a signal that completes immediately.
                return [RACSignal empty];
        }];
                
                
        toolBar.items = [NSArray arrayWithObjects:right,nil];
        textField.inputAccessoryView = toolBar;
                
        return;

    }
    
    if (_textFieldStyle == InputViewControllerTextFieldHotel || _textFieldStyle == InputViewControllerTextFieldSaleMan  || _textFieldStyle == InputViewControllerTextFieldPayStyle ) {
        UIPickerView *pickView = [[UIPickerView alloc] init];
        pickView.dataSource = self;
        pickView.delegate = self;
        
        
        pickView.tag = 1000+_textFieldStyle;
        
        
        textField.inputView  = pickView;
        
        
        
        // 建立 UIToolbar
        UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
        UIBarButtonItem *right = [[UIBarButtonItem alloc]init];
        [right setStyle:UIBarButtonItemStyleDone];
        right.title = NSLocalizedString(@"完成", nil);
        
        
        right.rac_command = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        
        
            [textField resignFirstResponder];
        
        
        
            // EVENT HANDLER code here
        
            // An empty signal is a signal that completes immediately.
            return [RACSignal empty];
        }];
                
                
        toolBar.items = [NSArray arrayWithObjects:right,nil];
        textField.inputAccessoryView = toolBar;
                
        return;
    }
    
    
    
    
    
}


-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    if (pickerView.tag-1000 == InputViewControllerTextFieldHotel) {
        return 2;
    }
    
    return 1;
}
-(NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{

    if (pickerView.tag-1000 == InputViewControllerTextFieldHotel) {
        switch (component) {
            case 0:
                return [HOTEL count];
                break;
            case 1:
                return [TING[_row] count];
                break;
            default:
                break;
        }
        
    }
    
    if (pickerView.tag-1000== InputViewControllerTextFieldSaleMan) {
        return [_saleMenArray count];
    }
    
    if (pickerView.tag-1000==InputViewControllerTextFieldPayStyle) {
        return [PAY count];
    }
    return 2;
}
-(NSString*) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{


    if (pickerView.tag -1000 ==InputViewControllerTextFieldHotel) {
            switch (component) {
            case 0:
                return HOTEL[row];
                break;
            case 1:
                return TING[_row][row];
                
                break;
            default:
                break;
        }
    }
    
    if (pickerView.tag-1000== InputViewControllerTextFieldSaleMan && [_saleMenArray count]>0) {
        
        return [NSString stringWithFormat:@"%@ %@",[_saleMenArray[row] objectForKey:@"name"],[_saleMenArray[row] objectForKey:@"jobNumber"]];
        
    }
    if (pickerView.tag-1000==InputViewControllerTextFieldPayStyle) {
        return [PAY objectAtIndex:row];
    }
    return @"";

}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    _row = [pickerView selectedRowInComponent:0];
    if (pickerView.tag-1000 == InputViewControllerTextFieldHotel) {
        
        [pickerView reloadComponent:1];
        NSInteger row1 = [pickerView selectedRowInComponent:1];
        _textField.text = [NSString stringWithFormat:@"%@.%@",HOTEL[_row],TING[_row][row1]];
        
        return;
    }
    if (pickerView.tag-1000==InputViewControllerTextFieldSaleMan && [_saleMenArray count]>0) {
        _textField.text = [NSString stringWithFormat:@"%@ %@",[_saleMenArray[row] objectForKey:@"name"],[_saleMenArray[row] objectForKey:@"jobNumber"]];
        
    }
    
    if (pickerView.tag-1000==InputViewControllerTextFieldPayStyle) {
        _textField.text = [PAY objectAtIndex:row];
    }
}

- (void)popVC{
    [_delegate inputViewController:self didPopWithString:_textField.text andKey:_textField.placeholder];
    
    
}

- (void)getSaleMen{
    __weak id weakSelf = self;
    
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    id token = [userDefaults objectForKey:@"token"];
    
    
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    
    [manager.requestSerializer setValue:[NSString stringWithFormat:@"Token %@",token] forHTTPHeaderField:@"Authorization"];
    [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [manager.requestSerializer setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    
    
    [manager POST:[[NetIP shareNetAddress] getSalesURLSring] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject){
        
        
        NSLog(@"%@",[responseObject objectForKey:@"saleman"]);
        [weakSelf performSelectorOnMainThread:@selector(setsalemen:) withObject:[responseObject objectForKey:@"saleman"] waitUntilDone:YES];
        
        
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];
    
}

- (void)setsalemen:(NSArray *)salemen
{
    _saleMenArray = salemen;

}
@end
